﻿using DevExpress.XtraEditors;
using DevExpress.XtraExport.Helpers;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using FastReport.Dialog;
using Mapster;
using Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Tools;
using 后整理管理系统.基本资料;
using 纺织贸易管理系统;
using 纺织贸易管理系统.选择窗体;

namespace 后整理管理系统.自定义类
{
  public  class SelectHelper
        
    {
        /// <summary>
        /// 选择原料
        /// </summary>
        /// <param name="gridView"></param>
        /// <param name="gridControl"></param>
        public static void SelectRawMaterial(GridView gridView, DevExpress.XtraGrid.GridControl gridControl)
        {
            if (gridView.FocusedRowHandle >= 0)
            {
                var fm = new 原料选择();
                fm.ShowDialog();
                var rowhandle = gridView.FocusedRowHandle;
                var danjumingxitables = gridControl.DataSource as List<ImportAndExportOfRawMaterials>;
                foreach (var raw in fm.SelecRaws)
                {
                    raw.Adapt(danjumingxitables[rowhandle]);
                    danjumingxitables[rowhandle].InventoryQuantity = 0;
                    danjumingxitables[rowhandle].InventoryQuantity1 = 0;
                    rowhandle++;
                }
                gridControl.RefreshDataSource();
                gridView.CloseEditor();
            }
        }
        public static void SelectCustomer(SelectParms parms)
        {
            var fm = new 客户选择();
            fm.ShowDialog();
            if (fm.linkman != null)
            {
               parms. Danju.ksbh = fm.linkman.BH;
               parms. Txtkehu.Text = fm.linkman.MC;
            }
        }
        public static void StockConverToDanjumingxi(GridView gridView1,List<StockTable > stocks ,List<danjumingxitable > danjumingxitables ,string danhao,DateTime rq,string formname)

        {
            var i = gridView1.FocusedRowHandle;
            //stockTables.AddRange(fm.pingzhong);
            foreach (var pingzhong in stocks)
            {
                if (danjumingxitables.Where(x =>x.rkdh==pingzhong.RKDH && x.Bianhao == pingzhong.BH && x.ganghao == pingzhong.GH && x.houzhengli == pingzhong.houzhengli && x.yanse == pingzhong.YS && x.OrderNum == pingzhong.orderNum && x.kuanhao == pingzhong.kuanhao && x.ContractNum == pingzhong.ContractNum).Count() > 0)
                {
                    if (Tools.ShowMessageBox.ShowAskBox($"缸号:" + pingzhong.GH + $"\r\n编号:{pingzhong.BH}\r\n后整理:{pingzhong.houzhengli}\r\n颜色:{pingzhong.YS}\r\n已经存在信息！是否继续添加") == DialogResult.No)
                    {
                        continue;
                    }
                }
                danjumingxitables[i].bizhong = "人民币￥";
                danjumingxitables[i].Bianhao = pingzhong.BH;
                danjumingxitables[i].guige = pingzhong.GG;
                danjumingxitables[i].chengfeng = pingzhong.CF;
                danjumingxitables[i].pingming = pingzhong.PM;
                danjumingxitables[i].kezhong = pingzhong.KZ;
                danjumingxitables[i].menfu = pingzhong.MF;
                danjumingxitables[i].danwei = "米";
                danjumingxitables[i].ContractNum = pingzhong.ContractNum;
                danjumingxitables[i].CustomName = pingzhong.CustomName;
                danjumingxitables[i].OrderNum = pingzhong.orderNum;
                danjumingxitables[i].kuanhao = pingzhong.kuanhao;
                danjumingxitables[i].houzhengli = pingzhong.houzhengli;
                danjumingxitables[i].yanse = pingzhong.YS;
                danjumingxitables[i].Chengpingyanse = pingzhong.YS;
                danjumingxitables[i].ganghao = pingzhong.GH;
                if (formname == DanjuLeiXing.销售出库单)
                {
                    danjumingxitables[i].chengpingjuanshu = pingzhong.yijianjuanshu;
                    danjumingxitables[i].toupimishu = pingzhong.MS;
                    danjumingxitables[i].chengpingmishu = pingzhong.biaoqianmishu;
                }
                else
                {
                    if(formname==DanjuLeiXing.委外加工单||formname==DanjuLeiXing.退卷单)
                    {
                        danjumingxitables[i].chengpingjuanshu = pingzhong.JS ;
                        danjumingxitables[i].toupimishu = pingzhong.MS;
                        danjumingxitables[i].chengpingmishu = pingzhong.MS;
                    }
                }
                danjumingxitables[i].Kuwei = pingzhong.Kuwei;
                danjumingxitables[i].Huahao = pingzhong.Huahao;
                danjumingxitables[i].ColorNum = pingzhong.ColorNum;
                danjumingxitables[i].CustomerColorNum = pingzhong.CustomerColorNum;
                danjumingxitables[i].CustomerPingMing = pingzhong.CustomerPingMing;
                danjumingxitables[i].rq =rq;
                var lxr = Connect.dbcontext.Queryable<LXR>().First(x => x.BH == pingzhong.CustomName);
                if (lxr != null)
                {
                    danjumingxitables[i].IsHanshui = Connect.dbcontext.Queryable<LXR>().First(x => x.BH == pingzhong.CustomName).DefualtTax ? "含税" : "未税";
                }
                else
                {
                    danjumingxitables[i].IsHanshui = IsHanshuiModel.未税 ;
                }
                danjumingxitables[i].suolv = pingzhong.ID;
                danjumingxitables[i].mashu = 100;
                danjumingxitables[i].rkdh = pingzhong.RKDH;
                //if (!string.IsNullOrWhiteSpace(pingzhong.IsCheckDone))
                //{
                //    danjumingxitables[i].IsDone = pingzhong.IsCheckDone.Contains("已");
                //}
                //else
                //{
                danjumingxitables[i].IsDone = true;
                //}
                danjumingxitables[i].offerid = pingzhong.LiuzhuanCard;
                danjumingxitables[i].hanshuidanjia = OrderDetailTableService.GetOneOrderDetailTable(x => x.OrderNum == pingzhong.orderNum && x.sampleNum == pingzhong.BH && x.Kuanhao == pingzhong.kuanhao
                && x.ColorNum == pingzhong.ColorNum && x.color == pingzhong.YS && x.Huahao == pingzhong.Huahao).price;
                danjumingxitables[i].hanshuiheji = danjumingxitables[i].hanshuidanjia * pingzhong.MS;
                i++;
                if (i == danjumingxitables.Count - 1)
                    for (int j = 0; j < 30; j++)
                    {
                        danjumingxitables.Add(new danjumingxitable() { danhao =danhao, rq = rq });
                    }
            }
        }
    public static DataTable GetSelect(DevExpress.XtraGrid.Views.Grid.GridView gridView)
        {
            var dt = GetFilterOrSortDatasOfGridView(gridView);
            var selectdt = new DataTable();
            try
            {
                selectdt = dt.Clone();
                selectdt.Rows.Clear();
                foreach (var row in gridView.GetSelectedRows())
                {
                    var newrow = selectdt.NewRow();
                    newrow.ItemArray = dt.Rows[row].ItemArray;

                    selectdt.Rows.InsertAt(newrow, 0);
                }
            }
            catch (Exception ex)
            {
                ShowMessageBox.ShowErrorBox(ex.Message);
            }
            return selectdt;
        }
        /// <summary>
        /// 获取GridView过滤或排序后的数据集
        /// </summary>
        /// <param name="gridView">gridView组件</param>
        /// <returns></returns>
        public static DataTable GetFilterOrSortDatasOfGridView(DevExpress.XtraGrid.Views.Grid.GridView gridView)
        {
            DataTable _dt = gridView.GridControl.DataSource as DataTable;
            if (_dt == null) return null;

            DataTable dt = _dt.Clone();
            for (int i = 0; i < gridView.RowCount; i++)
            {
                if (gridView.IsGroupRow(i))
                    continue;
                var dr = gridView.GetDataRow(i);
                if (dr == null)
                    continue;
                dt.Rows.Add(dr.ItemArray);
            }
            return dt;
        }
        /// <summary>
        /// 获取GridView过滤或排序后的数据集
        /// </summary>
        /// <typeparam name="T">泛型对象</typeparam>
        /// <param name="view">GridView</param>
        /// <returns></returns>
        public static List<T> GetGridViewFilteredAndSortedData<T>(DevExpress.XtraGrid.Views.Grid.GridView view) where T : class
        {
            var list = new List<T>();
            for (int i = 0; i < view.RowCount; i++)
            {
                if (view.IsGroupRow(i))
                    continue;
                var entity = view.GetRow(i) as T;
                if (entity == null)
                    continue;
                list.Add(entity);
            }
            return list;
        }
        public static List<T> GetSelectList<T>(DevExpress.XtraGrid.Views.Grid.GridView view) where T : class
        {
          var list=GetGridViewFilteredAndSortedData<T>(view);
            var selectList = new List<T>();
            try
            {
                foreach (var row in view.GetSelectedRows())
                {
                    selectList.Add(list[row]);
                }
            }
            catch (Exception ex){
                ShowMessageBox.ShowErrorBox(ex.Message);
            }
            return selectList;
        }
        /// <summary>
        /// 获取选中的卷
        /// </summary>
        /// <param name="juanList">所有卷</param>
        /// <param name="gridView2">表格</param>
        /// <returns></returns>
        public static List<JuanHaoTable> CreatJuanhao( GridView gridView2)
        {
            return SelectHelper.GetSelectList<JuanHaoTable>(gridView2).OrderBy(x => x.GangHao).ThenBy(x => x.PiHao).ToList();
        }
    }
}
